package org.dfl.plugin.views.palette.model;

import org.dfl.plugin.editor.tools.ActiveCreationTool;
import org.dfl.plugin.views.palette.model.AbstractPaletteEntry;
import org.eclipse.gef.Tool;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.gef.requests.SimpleFactory;
import org.eclipse.gef.tools.CreationTool;
import org.eclipse.jface.resource.ImageDescriptor;

/**
 * default entry with its image, label and tool that this entry provides
 * @author Piotr Wlodarczyk
 *
 */
public class DefaultPaletteEntry extends AbstractPaletteEntry {
	private Tool tool;
	private Object tansferTemplate;
	private String label;
	private ImageDescriptor image;
	
	public DefaultPaletteEntry(String label, ImageDescriptor image, Tool tool, CreationFactory tansferTemplate) {
		init(label, image, tool , tansferTemplate);
	}
	
	public DefaultPaletteEntry(String label, String imageSrc, Tool tool, CreationFactory tansferTemplate) {
		init(label, imageSrc, tool , tansferTemplate);
	}
	
	@SuppressWarnings("unchecked")
	public DefaultPaletteEntry(String label, String imageSrc,Class resultClass, boolean unloadOnFinish) {
		CreationFactory tansferTemplate = new SimpleFactory(resultClass);
		Tool tool = unloadOnFinish?new CreationTool(tansferTemplate):new ActiveCreationTool(tansferTemplate);
		init(label, imageSrc, tool, tansferTemplate);
	}
	
	public DefaultPaletteEntry(String label, String imageSrc, CreationFactory tansferTemplate, boolean unloadOnFinish) {
		Tool tool = unloadOnFinish?new CreationTool(tansferTemplate):new ActiveCreationTool(tansferTemplate);
		init(label, imageSrc, tool , tansferTemplate);
	}
	
	public DefaultPaletteEntry(String label,  ImageDescriptor image, CreationFactory tansferTemplate, boolean unloadOnFinish) {
		Tool tool = unloadOnFinish?new CreationTool(tansferTemplate):new ActiveCreationTool(tansferTemplate);
		init(label, image, tool , tansferTemplate);
	}
	
	public DefaultPaletteEntry(ToolEntry toolEntry) {
		/** tu ostatnim parametrem chyba nie powinien byc null! **/
		init(toolEntry.getLabel(), toolEntry.getSmallIcon(), toolEntry.createTool(), null);
	}

	protected void init(String label, String imageSrc,Tool tool, CreationFactory tansferTemplate) {
		init(label, createImage(imageSrc), tool, tansferTemplate);
	}

	protected void init(String label, ImageDescriptor image,Tool tool, CreationFactory tansferTemplate) {
		this.label = label;
		this.image = image;
		this.tansferTemplate = tansferTemplate;
		this.tool = tool;
	}

	@Override
	public ImageDescriptor getImage() {
		return image;
	}

	@Override
	public String getLabel() {
		return label;
	}

	@Override
	public Tool getTool() {
		return tool;
	}

	@Override
	public Object getTransferTemplate() {
		return tansferTemplate;
	}
}
